import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.Calendar;
import java.util.Scanner;
import java.util.Date;

public class BirthdayCheck {

	public BirthdayCheck() {
		try {
			String jar = getClass().getProtectionDomain().getCodeSource().getLocation().getFile().replaceAll("%20", " ");
			File jarFile = new File(jar);
			Scanner iF = new Scanner( new File(jarFile.getParent()+"/birthdays") );
			String s;
			String sYear;
			String sMonth;
			String sDay; 
			int i;
			while (iF.hasNext()) {
				s = iF.nextLine();
				for (i = 0; i<s.length() && s.charAt(i) != '#'; i++);
				sYear = s.substring(i+1, i+5);
				sMonth = s.substring(i+6, i+8);
				sDay = s.substring(i+9, i+11);
				Calendar cal = Calendar.getInstance();
				cal.setTime(new Date()); 
				int curYear = cal.get(Calendar.YEAR);
				int curMonth = cal.get(Calendar.MONTH)+1;
				int curDay = cal.get(Calendar.DAY_OF_MONTH);
				int curDate = 31*curMonth+curDay;
				int bDate = 31*Integer.parseInt(sMonth)+Integer.parseInt(sDay);
				if (bDate > curDate && bDate-curDate <=5) {
					JFrame parent = new JFrame();
   					JOptionPane.showMessageDialog(parent, s.substring(1,i-2)+" is getting "+(curYear-Integer.parseInt(sYear))+" years old on "+sMonth+"-"+sDay+".");
    			}
			}	
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Loading of birthday file failed.", "birthday calendar error", JOptionPane.WARNING_MESSAGE);
		}
		System.exit(0);
	}

	public static void main(String[] args) {
		new BirthdayCheck();
	}
}
